package com.bdqn.biz;
import com.bdqn.dao.UserDao;
import com.bdqn.pojo.UserPojo;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(name = "login", value = "/login")
public class Login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("post 方法");
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("name");
        String pass=request.getParameter("pass");

        String ck=request.getParameter("ck");
        //判断是否加入cookie
        if(ck!=null)
        {
            Cookie cookie=new Cookie("login",name);
            //生效于网站的整个路径
            cookie.setPath("/");
            cookie.setMaxAge(3600);
            response.addCookie(cookie);

        }

        try {
            UserPojo obj=UserDao.login(name,pass);
            //== 和equal 的区别
            if(obj.getName().equals(""))
            {
                response.sendRedirect("/Web1/login.jsp?error=ok");
            }
            else
            {
                //当登陆成功时，加入session
                HttpSession session=request.getSession();
                session.setAttribute("login",obj.getName());
                response.sendRedirect("/Web1/List.jsp?index=1");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
